<?php

/**
 * MVM_MALL 网上商店系统 商品评论
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-06-28 $
 * $Id: comment.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once './include/common.inc.php';
!$m_check_id &&  show_msg('login_please',GetBaseUrl('logging','login'));
if($ps_mode == 'comment_write') {
	if(!$comment_body) {
		show_msg('comment_body');
	}else if(strlen($comment_body)>500) {
		show_msg('comment_strlen');
	}
	//验证码
	if($mm_comment_code==1){
		require_once MVMMALL_ROOT.'./include/captcha.class.php';
		$Captcha = new  Captcha();
		$code    = trim($code);
		!$Captcha->CheckCode($code) &&	show_msg('code_wrong');
	}
	if($mm_comment_app==1){
		$approval_date  = 0;
	} elseif ($m_check_point>=$mm_comment_point) {
		$approval_date  = 1;
	}
	$ps_goid            = (int)$ps_goid;
	$comment_star       = (int)$comment_star;
	$subject            = dhtmlchars($subject);
	$comment_body       = dhtmlchars($comment_body);
	$query = "INSERT INTO `{$tablepre}gcomment_table` SET
                      guid              = '$ps_goid',
                      comment_id        = '$mm_group_id',
                      comment_name      = '$m_check_id',
                      comment_star      = '$comment_star',
                      comment_subject   = '$subject',
                      comment_body      = '$comment_body',
                      comment_ip        = '$m_user_ip',
                      approval_date     = '$approval_date',
                      register_date     = '$m_now_time'
                      ";
	$db->query($query);
	move_page(dreferer());
}
elseif($action=='reply' && is_numeric($uid)){
	if ($setup==1){
		strlen($reply_body)>500 &&  show_msg('comment_strlen');
		//验证码
		if($mm_comment_code==1){
			require_once MVMMALL_ROOT.'./include/captcha.class.php';
			$Captcha = new  Captcha();
			$code    = trim($code);
			!$Captcha->CheckCode($code) && show_msg('code_wrong');
		}
		$rows= array(
		            'com_id'      => $uid,
		            'user_id'     => $mm_group_id,
		            'user_name'   => $m_check_id,
		            'reply_date'  => $m_now_time,
		            'reply_body'  => $reply_body
		             );
		$db->insert("{$tablepre}reply_comment",dhtmlchars($rows));
		move_page(GetBaseUrl('product',$goods_id));
	}
	require_once MVMMALL_ROOT.'header.php';
	$navigation = make_navigation($lang['add_reply'],"comment.php?action=reply&uid=$uid");//导航
	$comment    = false;
	$comment    = $db->get_one("SELECT uid,guid,comment_subject,comment_body FROM `{$tablepre}gcomment_table` WHERE uid='$uid'");
	require_once template('reply_comment');
	footer();
}
elseif ($action=='view'&&is_numeric($id)){
	require_once MVMMALL_ROOT.'header.php';
	$product = $db->get_one("SELECT goods_name,uid FROM `{$tablepre}goods_table` WHERE uid=$id");
	!$product['uid'] && move_page(dreferer());
	//分页处理
	$total_count = $db->counter("{$tablepre}gcomment_table","guid='$id' AND approval_date>'0'");
	require_once MVMMALL_ROOT.'./include/pager.class.php';
	$page        = $page ? (int)$page : 1;
	$list_num    = 5;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();

	//商品评论
	$statr_one   = 0;
	$statr_two   = 0;
	$statr_three = 0;
	$statr_four  = 0;
	$statr_five  = 0;
	$star_nums   = 0;
	$result = $db->query("SELECT * FROM  `{$tablepre}gcomment_table` WHERE guid='$id' AND approval_date>'0' ORDER BY uid DESC LIMIT $from_record, $list_num");
	while ($rt = $db->fetch_array($result)) {
		$rt['title']         = dhtmlchars($rt['comment_subject']);
		$rt['conter']        = dhtmlchars($rt['comment_body']);
		$rt['name']          = dhtmlchars($rt['comment_name']);
		$rt['date']          = date($mm_date_format, $rt['register_date']);
		$rt['star']          = "<IMG  src='$imgpath/Egg_$rt[comment_star].gif'>";
		$rt['approval_date'] = $rt['approval_date']>1?date($mm_date_format,$rt['approval_date']):'';
		$rt['comment_id']    = $m_class_array[$rt['comment_id']];//显示会员等级
		$rep_sql = $db->query("SELECT * FROM  `{$tablepre}reply_comment` WHERE com_id='$rt[uid]' ORDER BY reply_id DESC");
		while ($rs = $db->fetch_array($rep_sql)) {
			$rs['reply_date']  = date($mm_date_format, $rs['reply_date']);
			$rs['user_id']     = $m_class_array[$rs['user_id']];
			$reply[]           = $rs;
		}
		$rt['reply']         = $reply;
		$comment[]             = $rt;
		unset($reply);
		++$star_nums;//总票数
		switch ($rt['comment_star']){
			case 1: {++$statr_one;break;}
			case 2: {++$statr_two;break;}
			case 3: {++$statr_three;break;}
			case 4: {++$statr_four;break;}
			case 5: {++$statr_five;break;}
			default: break; 
		}
	}
	$rebate_five  =  sprintf("%01.2f",($statr_five/$star_nums)*100) .'%';
	$rebate_four  =  sprintf("%01.2f",($statr_four/$star_nums)*100) .'%';
	$rebate_three =  sprintf("%01.2f",($statr_three/$star_nums)*100) .'%';
	$rebate_two   =  sprintf("%01.2f",($statr_two/$star_nums)*100) .'%';
	$rebate_one   =  sprintf("%01.2f",($statr_one/$star_nums)*100) .'%';
	//end
	if($rewrite==1){
		$baseurl  = "comment-view-$id-$page.html";
		$exc      ='.html';
	}else {
		$baseurl  = "comment.php?action=view&id=$id&page=";
	}
	$page_list    = $rowset->link($baseurl, $exc);
	$navigation   = make_navigation($lang['all_comment'],$baseurl.$page.$exc);//导航
	require_once template('comment_all');
	footer();
}
else {
	show_msg('pass_worng');
}
